<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\index\model\Ordera;
use app\index\model\Userinfo;
use app\index\controller\Product;

class Orders extends Controller
{
	public $callback;
	//订单接口
	public function index(Request $request){
		$data = $request->param();
		$this->callback = $data['callback'];
		// $user = new Userinfo;
		// $users = $user->show($data['id']);
		// if(empty($users)){
		// 	$arr = ['success'=>0,'msg'=>'请重新登录'];
		// 	return $this->callback.'('.json_encode($arr).')';
		// }
		$model = new Ordera;
		$orderNumber = $model->orderOne($data['id']);
		// echo $orderNumber;die;
		$product = new Product;
		$productOne = $product->getOne($data['productId']);
		// var_dump($productOne);die;
		if($productOne['endTime']<time()){
			// echo $productOne['endTime']."+++".time();die;
			$arr = ['success'=>2,'msg'=>'该产品以停卖'];
			echo $this->callback.'('.json_encode($arr).')';
		}
		if($productOne['surplusMoney']==0){
			$arr = ['success'=>2,'msg'=>'该产品已售罄'];
			echo $this->callback.'('.json_encode($arr).')';
		}
		if($productOne['surplusMoney']<$data['money']){
			$arr = ['success'=>2,'msg'=>'该产品剩余不足'];
			echo $this->callback.'('.json_encode($arr).')';
		}
		$in = ['addTime'=>time(),'uid'=>$data['id'],'pid'=>$data['productId'],'price'=>$data['money'],'orderNumber'=>$orderNumber];
		// print_r($in);die;
		$res = Db::table('order')->insert($in);
		if($res){
			$aa['orderNumber'] = $orderNumber;
			$aa['productName'] = $productOne['productName'];
			$aa['money'] = $data['money'];
			$aa['interestRate'] = $productOne['interestRate'];
			$aa['productId'] = $productOne['productId'];
			$arr = ['success'=>1,'data'=>$aa];
			echo $this->callback.'('.json_encode($arr).')';
		}else{
			$arr = ['success'=>0,'msg'=>'未知错误'];
			echo $this->callback.'('.json_encode($arr).')';
		}
	}
	public function orderSum(Request $request){
		$id = $request->param('goods');
    	$this->callback=$request->param('callback');
    	$data = Db::table('order')->where('pid',$id)->column('price');
    	var_dump(array_sum($data));die;
    	echo $this->callback.'('.json_encode($data).')';
	}
	public function sum($pid)
	{
		$data = Db::table('order')->where('pid',$pid)->column('price');
		echo array_sum($data);
	}
	//保存订单
	public function saveOrder(Request $request){
		$id = $request->param('orderNumber');
		$uid = $request->param('uid');
		$money = $request->param("surplusMoney");
		$cid = $request->param('pid');
		// echo $id;die;
    	$this->callback=$request->param('callback');
    	$res = Db::table('order')->where('orderNumber', $id)->update(['status' => 1,'paymentid'=>1]);
    	Db::table('product')->where('productId', $request->param('pid'))->setDec('surplusMoney', (int)$request->param('surplusMoney'));
    	Db::table('product')->where('productId', $request->param('pid'))->update(['updateTime' => time()]);
    	$data = array(
    		"time"=>time(),
    		"money"=>$money,
    		"cid"=>$cid,
    		"uid"=>$uid
    	);
    	Db::table("Investhistory")->insert($data);
    	if($res){
    		$arr = ['success'=>1];
			echo $this->callback.'('.json_encode($arr).')';
    	}else{
    		$arr = ['success'=>0,'msg'=>'未知错误'];
			echo $this->callback.'('.json_encode($arr).')';
    	}
	}
}